<?php

include "UserGroup.php";

class UserGroupDAO extends DataLayer {
 		
	/**
	* @desc Construtor da Classe
 	*/
	function __construct($lk){
		$this->lk = $lk;	
	}

	public function getUsersGroup($idUser=0, $idGroup=0, $page=0){
	
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							u.id_usuario, u.login, u.senha, u.nome, u.email, u.carga_horaria, DATE_FORMAT(u.data_criacao, '%d/%m/%Y') data_criacao, DATE_FORMAT(u.data_saida, '%d/%m/%Y') data_saida, u.status, u.data
							,ug.titulo grupo, ug.id_usuario_grupo
						FROM
							usuarios u				
						INNER JOIN
							usuarios_grupos_acl uga on uga.id_usuario = u.id_usuario
						INNER JOIN
							usuarios_grupos ug on ug.id_usuario_grupo = uga.id_usuario_grupo
						WHERE
							u.status IN (1,0)";

		if($idUser > 0)						
			$this->sql .= "  AND u.id_usuario = ".$idUser;

		if($idGroup > 0)						
			$this->sql .= "  AND uga.id_usuario_grupo = ".$idGroup;
		
		$this->sql .= " ORDER BY u.nome ASC";

		if($page > 0){
			$page = (int)$page;
			$page--;
			if($page < 0)
				$page = 0;
			else
				$page *= PAGINATION;

			$this->sql .= " LIMIT ".$page.",".PAGINATION;
		}
		
		$this->result = parent::select($this->sql);
		$userList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$userList[$i] = new UserGroup();
				$userList[$i]->setDadosDB($row);				

				$i++;
			}
		}
		
		return $userList;
	}

	public function getGroups($allGroups=false){
		$this->sql = "	SELECT 
							ug.id_usuario_grupo idUserGroup, 
							ug.titulo title,
							(	SELECT	
									COUNT(uga.id_usuario) 
								FROM
									usuarios_grupos_acl uga
								INNER JOIN
									usuarios u on u.id_usuario = uga.id_usuario
								WHERE
									u.status IN (1,0) AND uga.id_usuario_grupo = ug.id_usuario_grupo
							) users
						FROM 
							usuarios_grupos ug";
		
		if(!$allGroups){
			$this->sql .= " WHERE ug.id_usuario_grupo > 1";
		}					

		$this->result = parent::select($this->sql);

		return $this->result;
	}

	public function save($obj){
		if($obj != NULL){
			$idUser	 = (int)$obj["idUser"];
			$idGroup = (int)$obj["idGroup"];

			if($idUser > 0 && $idGroup > 0){
				$result = $this->getUsersGroup($idUser, $idGroup);

				if(count($result) > 0)
					$this->deleteUserGroup($idUser, $idGroup);
				
				$dbObj['id_usuario']		= $idUser;
				$dbObj['id_usuario_grupo']	= $idGroup;
				
				parent::insert("usuarios_grupos_acl",$dbObj);
				
				$result = $this->lk->sql_error();

				if($result["code"] == 0){
					return true;
				}										
			}
		}

		return false;
	}

	public function changeUserGroup($obj){
		if($obj != NULL){
			$idUser		= (int)$obj["idUser"];
			$idGroup	= (int)$obj["idGroup"];
			$idNewGroup	= (int)$obj["idNewGroup"];

			if($idUser > 0 && $idGroup > 0 && $idNewGroup > 0){
				
				if($this->deleteUserGroup($idUser, $idGroup)){
					$dbObj['id_usuario']		= $idUser;
					$dbObj['id_usuario_grupo']	= $idNewGroup;
					
					parent::insert("usuarios_grupos_acl",$dbObj);
					
					$result = $this->lk->sql_error();

					if($result["code"] == 0){
						return true;
					}	
				}				
			}
		}

		return false;
	}

	public function deleteUserGroup($idUser, $idGroup){
		$return = 0;

		if($idUser > 0 && $idGroup > 0){
			$return = parent::delete("usuarios_grupos_acl","id_usuario = ".$idUser." AND id_usuario_grupo = ".$idGroup);
		}

		return $return;
	}
}

?>